﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ServerListPanel : BasePanel<ServerListPanel>
{
    public UIScrollView svLeft;
    public UIScrollView svRight;
    public UILabel labTitle;
    public RightServerTab serTab;
    public UIButton btnBack;
    public UIScrollBar sbLeft;
    public void RemoveAllChildren(GameObject parent)
    {
        Transform transform;
        for (int i = 0; i < parent.transform.childCount; i++)
        {
            transform = parent.transform.GetChild(i);
            GameObject.Destroy(transform.gameObject);
        }
    }

    public override void Init()
    {
        //生成测试数据
        //ServerMgr.Instance.CreateTestData();
        Hide();
        btnBack.onClick.Add(new EventDelegate(()=>
        {
            Hide();
            ServerPanel.Instance.Show();
        }));
        //初始化上次登录的服务信息
        this.InitLastServerInfo();
        //创建左侧列表
        int maxPage = ServerMgr.Instance.GetMaxPage(10);
        int maxCount = ServerMgr.Instance.GetCount();
        for (int i = 0; i < maxPage; i++)
        {
            GameObject obj = Instantiate(Resources.Load<GameObject>("Prefab/LeftServerTab"));
            obj.transform.SetParent(svLeft.transform, false);
            obj.transform.localPosition = new Vector3(-70, 120 + (-60 * i), 0);
            LeftServerTab ser = obj.GetComponent<LeftServerTab>();
            ser.Init(i * 10 + 1, Mathf.Min(i * 10 + 10, maxCount));
        }
        svLeft.ResetPosition();
        //初始化
        this.ChangeServerList(1, 10);
        //svLeft.SetDragAmount(-239, -31, true);
        //sbLeft.value = 0;
    }

    public void InitLastServerInfo()
    {
        if (AccountMgr.Instance.lastServerId != 0)
        {
            ServerItem item = ServerMgr.Instance.GetServer(AccountMgr.Instance.lastServerId);
            if (item != null)
            {
                serTab.Init(item);
            }
        }
    }

    public void ChangeServerList(int startId, int endId)
    {
        this.RemoveAllChildren(svRight.gameObject);
        labTitle.text = string.Format("服务器 {0}-{1}", startId, endId);
        List<ServerItem> list = ServerMgr.Instance.GetServerList(startId, endId);
        //创建右侧列表
        for (int i = 0; i < list.Count; i++)
        {
            GameObject obj = Instantiate(Resources.Load<GameObject>("Prefab/RightServerTab"));
            obj.transform.SetParent(svRight.transform, false);
            //通过代码来自己设置位置(将svRight节点的Grid脚本关闭)(注：坐标是由下至上，也就是添加的顺序是反的)
            obj.transform.localPosition = new Vector3(298 * (i % 2), -73 * (i / 2), 0);
            RightServerTab ser = obj.GetComponent<RightServerTab>();
            ser.Init(list[i]);
        }
        svRight.UpdateScrollbars();
    }

    public void LoginServer(int serverId)
    {
        Hide();
        //登录服务ID
        AccountMgr.Instance.LoginServer(serverId);
        ServerPanel.Instance.Show();
    }

    public override void Show()
    {
        base.Show();
        this.InitLastServerInfo();
        this.ChangeServerList(1, 10);
    }
}
